<?php
/**
 * 执行点赞入库，需要fabulous_cron_exec.php执行
 */
require_once dirname(dirname(__DIR__))."/common.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\UserZan;
//实例化数据库
$db =  SysResource::getDB(true);
$UserZan = new UserZan($db);
$UserZan->setDb($db);
//实例化redis
$redis = SysResource::getRedis();
//这里有5个队列,所以循环5次
for($i=1;$i<=5;$i++){
    //每个队列再去循环100次取数据
    for($j=1;$j<=100;$j++){
        //正式key
//        $key = REDIS_PREFIX."fabulous_".$i;
        //测试key
        $key = REDIS_PREFIX."test_fabulous_".$i;
        //从队列中弹出一个值
        $res=$redis->rPop($key);
        //把对象变为数组
        $data=json_decode($res,true);
        if(!empty($data)){
            //添加新增时间
            $data['add_time']=date("Y-m-d H:i:s",time());
            //先去数据库查询该用户是否点赞了该视频，点赞就不在点赞了
            $db->select("id");
            $db->from("material_zan");
            $db->where(['my_uuid'=>$data['my_uuid'],'other_uuid'=>$data['other_uuid'],'video_uuid'=>$data['video_uuid']]);
            $query =  $db->get();
            $datas = $query->one();
//            var_dump($datas);exit;
            if(!$datas){
                //加入数据库
                $db->insert("material_zan",$data);
                $userId = $db->insert_id();
            }else{
                $userId = true;
            }
//            //加入数据库
//            $db->insert("material_zan",$data);
//            $userId = $db->insert_id();
            //添加失败再去重新入队列
            if(!$userId){
                if($i==1){
//                    $key = REDIS_PREFIX."fabulous_5";
                    $key = REDIS_PREFIX."test_fabulous_5";
                    $redis->lPush($key,json_encode($data));
                }else if($i==2){
//                    $key = REDIS_PREFIX."fabulous_1";
                    $key = REDIS_PREFIX."test_fabulous_1";
                    $redis->lPush($key,json_encode($data));
                }else if($i==3){
//                    $key = REDIS_PREFIX."fabulous_2";
                    $key = REDIS_PREFIX."test_fabulous_2";
                    $redis->lPush($key,json_encode($data));
                }else if($i==4){
//                    $key = REDIS_PREFIX."fabulous_3";
                    $key = REDIS_PREFIX."test_fabulous_3";
                    $redis->lPush($key,json_encode($data));
                }else if($i==5){
//                    $key = REDIS_PREFIX."fabulous_4";
                    $key = REDIS_PREFIX."test_fabulous_4";
                    $redis->lPush($key,json_encode($data));
                }
            }
        }else{
            continue;
        }

    }
    sleep(1);
}

?>